package com.atguigu.auth.controller;
/*创建接口
 *测试controller接口
 * 查询所有角色
 * http://localhost:8800/admin/system/sysRole/findAll
 * */
import com.atguigu.auth.Service.SysRoleService;
import com.atguigu.common.exception.GuiguException;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    /*
    http://localhost:8800/admin/system/sysRole/findAll
    注入service
    */
    @Autowired
    private SysRoleService sysRoleService;
    //查询所有角色
/*
    @GetMapping("/findAll")
//  首次代码  返回list集合
    public List<SysRole> findAll(){
        //调用service的方法
        List<SysRole> list = sysRoleService.list();
        return list;
    }
*/
/*1 查询所有角色    和当前用户所属角色*/
    @ApiOperation("获取角色")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
      Map<String,Object> map = sysRoleService.findRoleDataByUserId(userId);
      return Result.success(map);
    }
/*2为用户分配角色   一对一或一对多*/
    @ApiOperation("为用户分配角色")
    @PostMapping("/doAssign")
        public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
            sysRoleService.doAssign(assginRoleVo);
            return Result.success();
        }


    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    //    一次改造  返回Result
    public Result findAll() {
        //调用service的方法
        List<SysRole> list = sysRoleService.list();
        //模拟异常
        try {
//            int  i = 10/0;
        }catch (Exception e){
            throw new GuiguException(2001,"执行了自定义异常处理");

        }

        return Result.success(list);
    }

    //条件分页查询
    //page表示当前页 limit表示每页显示记录数
    //SysRoleQueryVO    条件对象
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page,
                                @PathVariable Long limit,
                                SysRoleQueryVo sysRoleQueryVo){
        //调用service方法
        //1创建page对象  传递分页参数
        Page<SysRole> pageParam = new Page<>(page, limit);
        //2封装条件  判断条件是否为空    不为空封装
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
       String roleName =  sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)){
            //封装
            wrapper.like(SysRole::getRoleName, roleName);
        }
        //实现
        Page<SysRole> pageModel = sysRoleService.page(pageParam, wrapper);
        return Result.success(pageModel);
    }



    //添加角色
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result save(@RequestBody SysRole role){
        //调用service的方法
    boolean is_success = sysRoleService.save(role);
    if (is_success) {
        return Result.success();
    }else {
        return Result.fail();
    }


}
    //修改角色  根据id查询
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        SysRole SyRole = sysRoleService.getById(id);
        return Result.success(SyRole);
    }
//修改角色  最终实现
@PreAuthorize("hasAuthority('bnt.sysRole.update')")
@ApiOperation("修改角色")
@PutMapping("update")
public Result update(@RequestBody SysRole role) {
    //调用service的方法
    boolean is_success = sysRoleService.updateById(role);
    if (is_success) {
        return Result.success();
    } else {
        return Result.fail();
    }
    }
    //删除    根据id删除
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id删除")
    @DeleteMapping("remoove/{id}")
    public Result remove(@PathVariable Long id){
        boolean is_success = sysRoleService.removeById(id);
        if (is_success) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }
//    批量删除

//    前端用数组传递 后端用集合接收
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
       boolean is_success = sysRoleService.removeByIds(idList);
       if (is_success) {
           return Result.success();
       }else {
           return Result.fail();
       }
   }


}
